//
//  LoanCalculator.m
//  Rong360
//
//  Created by tianliwei on 13/2/14.
//  Copyright (c) 2014 tianliwei. All rights reserved.
//

#import "LoanCalculator.h"
@implementation LoanCalculator

- (void)beginCalculate{
    
    LoanHisUnit *loanHis = [[LoanHisUnit alloc]init];
    //商贷金额
    loanHis.bloan = self.bloan;
    int bloan = [self.bloan integerValue];
    
    //公积金金额
    loanHis.gloan = self.gloan;
    int gloan = [self.gloan integerValue];
    
    int loan = bloan + gloan;
    NSString *totalLoan = [NSString stringWithFormat:@"%d",loan];
    
    //贷款期限
    loanHis.deadline = self.deadline;
    int deadline = [self.deadline integerValue];
    
    
    //商代利率
    loanHis.brate = self.brate;
    CGFloat brate = [self.brate floatValue];
    
    
    //公积金利率
    loanHis.grate = self.grate;
    CGFloat grate = [self.grate floatValue];
    
    
    //贷款期限不能为0
    if ( deadline == 0) return;
    
    
    //商贷和公积金贷款计算不能都为0
    if ( bloan * brate == 0 && gloan * grate == 0) return;
    
    //如果输入了公积金金额，公积金利率就不能为零，如果输入了商贷金额，商贷利利率就不能为零
    if ( (bloan > 0) &&  (brate == 0) ) return;
    if ( (gloan > 0) &&  (grate == 0) ) return;
    
    
    //商贷部分
    CGFloat p = ( bloan * 10000.0 ); //本金(单位万)
    CGFloat R = ( brate / 1200.0); //月利率
    CGFloat N = (deadline * 12.0);//还款期数
    
    //商贷等额本息 bx
    CGFloat cmonth_b_bx = ( p * R * pow((1.0 +R),N) ) / ( pow(1.0 + R, N) - 1.0 ) ;//每月还款
    if (isnan(cmonth_b_bx)) {
        cmonth_b_bx = 0.0;
    }
    CGFloat ctotal_b_bx = cmonth_b_bx * N ;//本息合计
    CGFloat crate_b_bx = ctotal_b_bx - p;//总利息
    
    //商贷等额本金 bj
    CGFloat cmonth_b_bj = p / N + p * R;
    if (isnan(cmonth_b_bj)) {
        cmonth_b_bj = 0.0;
    }
    CGFloat cmondec_b_bj = p / N *R;
    CGFloat crate_b_bj = ((p / N + p * R) + p / N * (1 + R)) / 2 * N - p;
    CGFloat ctotal_b_bj = p + crate_b_bj;
    //公积金部分
    CGFloat p2 = ( gloan * 10000.0 ); //本金(单位万)
    CGFloat R2 = ( grate / 1200.0); //月利率
    CGFloat N2 = ( deadline * 12.0);//还款期数
    
    //公积金等额本息
    CGFloat cmonth_g_bx = ( p2 * R2 * pow((1.0 + R2),N2) ) / ( pow( (1.0 + R2) , N2) - 1.0 ) ;//每月还款
    if ( isnan(cmonth_g_bx)) {
        cmonth_g_bx = 0.0;
    }
    CGFloat ctotal_g_bx = cmonth_g_bx * N2 ;//本息合计
    CGFloat crate_g_bx = ctotal_g_bx - p2;//总利息
    
    //公积金等额本金
    CGFloat cmonth_g_bj = p2 / N2 + p2 * R2;
    if (isnan(cmonth_b_bj)) {
        cmonth_g_bj = 0.0;
    }
    CGFloat cmondec_g_bj = p2 / N2 * R2;
    CGFloat crate_g_bj = ((p2 / N2 + p2 * R2) + p2 / N2 * (1 + R2)) / 2 * N2 - p2;
    CGFloat ctotal_g_bj = p2 + crate_g_bj;
    loanHis.cmondec_bj = [NSString stringWithFormat:@"%d",(int)(cmondec_g_bj+cmondec_b_bj)];
    loanHis.cmonth_bx = [NSString stringWithFormat:@"%d",(int)(cmonth_b_bx+cmonth_g_bx)];
    loanHis.ctotal_bx = [NSString stringWithFormat:@"%d",(int)(ctotal_b_bx+ctotal_g_bx)];
    loanHis.crate_bx = [NSString stringWithFormat:@"%d",(int)(crate_b_bx+crate_g_bx)];
    loanHis.cmonth_bj = [NSString stringWithFormat:@"%d",(int)(cmonth_b_bj+cmonth_g_bj)];
    loanHis.ctotal_bj = [NSString stringWithFormat:@"%d",(int)(ctotal_b_bj+ctotal_g_bj)];
    loanHis.crate_bj = [NSString stringWithFormat:@"%d",(int)(crate_b_bj+crate_g_bj)];
    
    self.result_bx = @[loanHis.cmonth_bx,loanHis.crate_bx,loanHis.ctotal_bx,totalLoan,loanHis.deadline];
    self.result_bj = @[loanHis.cmonth_bj,loanHis.crate_bj,loanHis.ctotal_bj,totalLoan,loanHis.deadline,loanHis.cmondec_bj];
    
        
    //保存数据库
    
    DBMan* dbm = [DBMan shareDB];
    [dbm saveToLoanHis:loanHis];
}

@end
